﻿using AdminStyem.Application;
using AdminStyem.Application.Command.LoginCommand;
using AdminStyem.Application.Command.traceabilityCommand;
using AdminStyem.Application.DTO;
using AdminStyem.Domain.Entity;
using MediatR;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.OpenXmlFormats.Dml.Spreadsheet;
using NPOI.SS.Formula.Functions;
using AdminStyem.Application.Command.CultivationCommand;
using AdminStyem.Application.Command.SlaughterCommand;
using AdminStyem.Application.Command.ProcessingCommand;
using NPOI.OpenXmlFormats.Dml.Diagram;
using AdminStyem.Application.Command.FeedstockCommand;

namespace AdminStyem.WebAPI.Controllers
{
   /// <summary>
   /// 控制器
   /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class oneController : ControllerBase
    {
        IMediator mediator;

        public oneController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserName">账号</param>
        /// <param name="UserPass">密码</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public  async Task<APIResult<List< UserRoleDTO>>> Login(string? userName,string? userPass)
        {
            LoginCommand loginCommand = new LoginCommand();
            loginCommand.UserName = userName;
            loginCommand.UserPass = userPass;
            return await mediator.Send(loginCommand);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="traceabilityAddCommand">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public  async Task<APIResult<object>> AddTraceability(TraceabilityAddCommand traceabilityAddCommand)
        {
            return await mediator.Send(traceabilityAddCommand);
        }
        /// <summary>
        /// 上传照片
        /// </summary>
        /// <param name="file">照片文件</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public IActionResult UploadPhoto(IFormFile file)
        {
            var filePath =Directory.GetCurrentDirectory()+@"\wwwroot\file\" + file.FileName;

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                 file.CopyTo(stream);
            }
            var url="/file/"+ file.FileName;
            return Ok(url);
        }
        /// <summary>
        /// 溯源显示分页
        /// </summary>
        /// <param name="pageSize">条</param>
        /// <param name="pageIndex">页</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult<APiPaging<TrcatebilityCommodityDTO>>> ShowTraceability(int pageSize, int pageIndex)
        {
            TraceabilityShowCommand traceabilityShowCommand = new TraceabilityShowCommand();
            traceabilityShowCommand.PageSize = pageSize;
            traceabilityShowCommand.PageIndex = pageIndex;
            return await mediator.Send(traceabilityShowCommand);
        }
       /// <summary>
       /// 溯源反填
       /// </summary>
       /// <param name="TraceabilityId">溯源id</param>
       /// <returns>返回任务</returns>
       /// <returns>返回任务</returns>
        [HttpGet]
        public  async Task<APIResult<object>> FTTraceability(int TraceabilityId)
        {
            TraceabilityFTCommand traceabilityFTCommand = new TraceabilityFTCommand();
            traceabilityFTCommand.TraceabilityId = TraceabilityId;
           return await mediator.Send(traceabilityFTCommand);
        }
        /// <summary>
        /// 获取商品
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult <List< Commodity>>> ShowCommunity()
        {
            CommodityCommand commodityCommand = new CommodityCommand();
           return await mediator.Send(commodityCommand);
        }
        /// <summary>
        ///删除溯源
        /// </summary>
        /// <param name="command">ID</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public  async Task<APIResult<object>> DelTraceability(TraceabitlDelCommand command)
        {
            return await mediator.Send(command);    
        }
        /// <summary>
        /// 修改溯源
        /// </summary>
        /// <param name="command"></param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdateTraceability(TraceabilityupdateCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 导出溯源数据到Excel
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<IActionResult> ExportTraceabilityDataToExcel()
        {
            var traceabilityShowCommand = new TraceabilityShowCommand
            {
                PageSize = int.MaxValue, // 导出所有数据
                PageIndex = 1
            };

            var result = await mediator.Send(traceabilityShowCommand);

            //if (result.Code != ResultCode.添加成功)
            //{
            //    return BadRequest(result.Message);
            //}

            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("TraceabilityData");

            // 创建表头
            IRow headerRow = sheet.CreateRow(0);
            headerRow.CreateCell(0).SetCellValue("TraceabilityId");
            headerRow.CreateCell(1).SetCellValue("CommodityId");
            headerRow.CreateCell(2).SetCellValue("TraceabilityBatch");
            headerRow.CreateCell(3).SetCellValue("TraceabilityShelf");
            headerRow.CreateCell(4).SetCellValue("TraceaDocuments");
            headerRow.CreateCell(5).SetCellValue("Manufacturers");
            headerRow.CreateCell(6).SetCellValue("TraceabilityPhoto");
            headerRow.CreateCell(7).SetCellValue("CommodityName");

            // 填充数据
            int rowIndex = 1;
            foreach (var item in result.Data.list)
            {
                IRow row = sheet.CreateRow(rowIndex);
                row.CreateCell(0).SetCellValue(item.TraceabilityId);
                row.CreateCell(1).SetCellValue(item.CommodityId);
                row.CreateCell(2).SetCellValue(item.TraceabilityBatch);
                row.CreateCell(3).SetCellValue(item.TraceabilityShelf);
                row.CreateCell(4).SetCellValue(item.TraceaDocuments);
                row.CreateCell(5).SetCellValue(item.Manufacturers);
                row.CreateCell(6).SetCellValue(item.TraceabilityPhoto);
                row.CreateCell(7).SetCellValue(item.CommodityName);
                rowIndex++;
            }

            var fileName = $"TraceabilityData_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
            var filePath = Path.Combine("wwwroot", "exports", fileName);

            // 确保目录存在
            Directory.CreateDirectory(Path.GetDirectoryName(filePath));

            using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                workbook.Write(fileStream);
            }
            var fileUrl = $"/exports/{fileName}";
            return Ok(new { FileUrl = fileUrl });
        }
        /// <summary>
        /// 养殖信息添加
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
       public async Task<APIResult<object>> CultivationAdd(CultivationAddCommand command)
        {
            return await mediator.Send(command);    
        }
        /// <summary>
        /// 养殖信息显示
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult<List<CultivationDTO>>> CultivationShow()
        {
            CultivationShowCommand command = new CultivationShowCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖企业添加
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public  async Task<APIResult<object>> BreedBassAdd(BreedingAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 养殖企业显示
        /// </summary>
        /// <returns>实体</returns>
        [HttpGet]
        public async Task<APIResult<List<BreedBassesDTO>>> BreedShow()
        {
            BreedingShowCommand command = new BreedingShowCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 疫苗
        /// </summary>
        /// <returns>实体</returns>
        [HttpGet]
        public async Task<APIResult<List<VaccineDTO>>> Vaccion()
        {
            VaccineShowCommand command=new VaccineShowCommand();
            return await mediator.Send(command);    
        }
       
        /// <summary>
        /// 屠宰添加
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<object>> SlauggterAdd(SlaughteraddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        ///屠宰显示
        /// </summary>
        /// <returns>实体</returns>
        [HttpGet]
        public  async Task<APIResult<List<SlaughterDTO>>> SlauggterShow()
        {
            SlaughterShowCommand command=new SlaughterShowCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 屠宰公司添加
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<object>> SlauggterBassAdd(SlaughterBassAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 屠宰公司显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<SlaughteringfirmDTO>>> SlaughterBassShow()
        {
            SlaughteringfirmShowCommand command = new SlaughteringfirmShowCommand();
            return await mediator.Send(command);
        }

        /// <summary>
        /// 深加工信息
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<object>> ProcessingAdd(ProcessingaddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 深加工信息显示
        /// </summary>
        /// <returns>实体</returns>
        [HttpGet]
        public  async Task<APIResult<List<ProcessingDTO>>> ProcessingShow()
        {
           ProcessingShowCommand command=new ProcessingShowCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 深加工公司
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<object>> ProcessingFrimAdd(ProcessingFirmAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 深加工显示
        /// </summary>
        /// <returns>实体</returns>
        [HttpGet]
        public async Task<APIResult<List< ProcessingFirmDTO>>> ProcessingBassShow()
        {
            ProcessingFirmShowCommand command=new ProcessingFirmShowCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 原料添加
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public  async Task<APIResult<object>> FeedstorkAdd(FeedstockAddCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 原料显示
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult<List<FeedbackDTO>>> GetFeefstorkShow()
        {
            FeedstockShowCommand command=new FeedstockShowCommand();
            return await mediator.Send(command);
        }
        

    }
}

